Contributors: Javier Burgoa (Flanders Marine Institute)
Source data: Data were made available by EMODnet Physics, in collaboration with Copernicus Marine Service (CMEMS) and EuroGOOS ROOSs INSTAC where data originator and data center is: Agency for Maritime Services and Coast Department Coast (MDK) for all 4 stations.
Institute: LifeWatch (www.lifewatch.be)

This following code makes use of EMODnet’s available data on wave height at different stations to produce interactive time series plots. To download the data, go to www.emodnet-physics.eu/Map/ selecting as parameter ‘Wave’ to include only stations that measure wave height. Data can be taken from several stations, however make sure that the span of time is the same for all stations.
Once the data is downloaded there should be one folder per station and several excel files (one per day of recording) within each station folder. Set working directory to the folder containing all downloaded and un-zipped data.

1. Load the dataset

Load dependencies

library(plyr)
library(plotly)

Get data from folders in CSV format and remove all non wanted data.

files <- list.files(path = "./", recursive = TRUE, 
    pattern = NULL)  ## path to folder/s with all files

data <- sapply(files, read.csv2, header = T, 
    comment.char = "#", simplify = FALSE)

2. Data preparation

In this following chunk the variable of interest is selected for plotting (default is average wave height of the 1/10th highest waves coded as “VH110”, but any variable not ending in "_QC" in the excel sheets can be chosen). Then all tables will be combined into a single one with wave heights separated in columns based on the station they come from, and time as first variable as in:

Time Station_A Station_B
20-Oct-2020 12:00 5.5 9.0
20-Oct-2020 12:30 5.6 9.0
20-Oct-2020 13:00 5.5 9.2
ChosenVar <- "VH110"  # Substitute here name of variable to plot (non ending in '_QC')


data <- lapply(data, FUN = function(dataset) {
    # Remove invalid columns and
    # duplications of data
    dataset[which(dataset[, "DEPTH"] == 
        0), ]
    dataset <- dataset[, c("TIME", ChosenVar)]  # eliminate non-used variables
    dataset[which(dataset[, ChosenVar] != 
        "--"), ]  # eliminate duplicates
    
})

# Add station to each dataset
data <- lapply(names(data), FUN = function(nam) {
    cbind(data[[nam]], LOCATION = gsub("_All.*", 
        "", nam))
})

## Combine all datasets
data <- do.call(rbind, data)

## Separate datasets based on station
data <- split(data, data$LOCATION)


data <- lapply(data, function(dataset) {
    # Change names of variable column by
    # the name of the station
    colnames(dataset)[grep(ChosenVar, colnames(dataset))] <- unique(dataset[["LOCATION"]])
    dataset[!(names(dataset) == "LOCATION")]  # eliminate location variable
})

## Merge
data <- Reduce(function(data1, data2) merge(data1, 
    data2, by = "TIME", all = TRUE), data)

## Turn Wave height values into numbers
## and take seconds out of variable
## 'TIME'
data[, which(colnames(data) != "TIME")] <- apply(data[, 
    which(colnames(data) != "TIME")], 2, 
    function(col) {
        col <- gsub(",", ".", col)
        col <- as.numeric(col)
        col <- gsub(":00$", "", gsub(":000$", 
            "", col))
        return(col)
    })
data$TIME <- as.POSIXct(data$TIME, format = "%d/%m/%Y %H:%M:%S")  # time as date type of value

3. Time series plotting

Plotting is made with the package “plotly”. If you wish to change any aesthetics of the plot please go to the plotly webpage for more information. If you wish to reduce the number of stations plotted, please change to the “i” values of the loop.

p <- plot_ly()
for (i in 2:ncol(data)) {
    ## this loop adds one by one the
    ## different columns in our dataset
    ## (data from each station)
    p <- add_trace(p, x = data[["TIME"]], 
        y = data[[i]], type = "scatter", 
        mode = "lines+marker", name = colnames(data)[i])
}

## To add axis and title simply fill in
## the following code
layout(p, title = "Average height of the heighest 1/10th of the waves", 
    xaxis = list(title = "Time", showgrid = FALSE), 
    yaxis = list(title = "Wave height", 
        showgrid = TRUE))